package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.po.Menu;
import com.example.demo.service.MenuService;

@Controller
@RequestMapping("/admin")
public class MenuController {
	
	@Autowired
	private MenuService menuService;

	@RequestMapping("/adminMenu.action")
	public String list(Model model) {

		List<Menu> menuDtos = menuService.selectAll();
		model.addAttribute("menuDtos", menuDtos);
		
		return "admin/menu/menuList";
	}
	@RequestMapping("/toedit")
	public String toedit(Model model, Integer id) {

		List<Menu> menuDtos = menuService.getMenu();
		model.addAttribute("menuDtos", menuDtos);
		if (id != null) {
			Menu menu = menuService.selectById(id);

			model.addAttribute("menu", menu);
		}

		return "admin/menu/edit";
	}
	@RequestMapping("/edit")
	public String edit(Menu menu) {

		if (menu.getId() != null ) {

			menuService.update(menu);

		} else {
			menuService.insert(menu);

		}

		return "redirect:adminMenu.action";
	}
	@RequestMapping("/delete")
	public String delete(Integer id) {

		if (id != null ) {

			menuService.delete(id);

		}

		return "redirect:adminMenu.action";

	}

}
